"""
doc
"""

from django.db import models
from django.utils import timezone

from app_base.models import BaseModel

from app_common import app_uuid


# Create your models here.
class WxUser(BaseModel):
    """doc"""

    openid = models.CharField(max_length=50)
    nick_name = models.CharField(max_length=50)
    telephone = models.CharField(max_length=50)

    #
    head_img_url = models.CharField(max_length=255)
    # 默认登录的小区
    default_community_uuid = models.UUIDField(default="0" * 32)
    default_house_uuid = models.UUIDField(default="0" * 32)

    class Meta:
        db_table = "WxUser"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["openid"]),
            models.Index(fields=["nick_name"]),
            models.Index(fields=["telephone"]),
        ]


class WxUserHouse(BaseModel):
    """doc"""

    user_uuid = models.UUIDField()
    house_uuid = models.UUIDField()

    class Meta:
        db_table = "WxUserHouse"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["user_uuid"]),
            models.Index(fields=["house_uuid"]),
        ]


class WxUserRechargeOrder(BaseModel):
    """doc"""

    # base info
    class OrderState(models.TextChoices):
        """doc"""

        # 待支付, 从 1 开始
        WAIT = "未支付"
        # 支付成功
        SUCCESS = "成功"
        # 支付成功
        FAIL = "失败"
        # 已经退款
        REFUND = "已退款"

    # 充值订单
    openid = models.CharField(max_length=50)
    # 订单uuid
    out_trade_no = models.UUIDField(
        default=app_uuid.hex_uuid,
    )
    house_uuid = models.UUIDField()
    house_bill_uuid = models.UUIDField()
    # 金额单位：分
    total_fee = models.PositiveIntegerField()
    # 参考 OrderState
    state = models.CharField(
        default=OrderState.WAIT.value, choices=OrderState.choices, max_length=20
    )

    pay_at = models.DateTimeField(default=timezone.now)

    # 账单详情
    order_title = models.CharField(max_length=255)

    class Meta:
        db_table = "WxUserRechargeOrder"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["openid"]),
            models.Index(fields=["state"]),
            models.Index(fields=["out_trade_no"]),
            models.Index(fields=["house_bill_uuid"]),
            models.Index(fields=["pay_at"]),
        ]
